<?php
// Submits new and changed inquiry data to the database.

session_start();
if ( ! isset( $_SESSION['adminLoggedIn'] ) ) {
   exit;
}

require_once("db.php");
  
// Is there data coming in?
if ( isset($_POST['primaryKey'] ) ) {

   $primaryKey = $_POST['primaryKey'];
   $firstname = mysql_real_escape_string($_POST['firstname']);
   $lastname = mysql_real_escape_string($_POST['lastname']);
   $sid = mysql_real_escape_string($_POST['sid']);
   $birthday = mysql_real_escape_string($_POST['birthday']);
   $accepted_on = mysql_real_escape_string($_POST['accepted_on']);
   $toefl = mysql_real_escape_string($_POST['toefl']);
   $gre_verbal = mysql_real_escape_string($_POST['gre_verbal']);
   $gre_quant = mysql_real_escape_string($_POST['gre_quant']);
   $gre_aw = mysql_real_escape_string($_POST['gre_aw']);
   $university = mysql_real_escape_string($_POST['university']);
   $grade = mysql_real_escape_string($_POST['grade']);
   $country = mysql_real_escape_string($_POST['country']);
   $email = mysql_real_escape_string($_POST['email']);
   $db_track = $_POST['db_track'];
   $ds_track = $_POST['ds_track'];
   $is_track = $_POST['is_track'];
   $oo_track = $_POST['oo_track'];
   $se_track = $_POST['se_track'];
   $mbi_track = $_POST['mbi_track'];

   // Are we inputing new data?
   if ( $primaryKey == '-1') {
                
      $query = "insert into gs_students 
         (firstname, lastname, sid, birthday, accepted_on, toefl, gre_verbal, 
            gre_quant, gre_aw, university, grade, country, email, db_track,
            ds_track, is_track, oo_track, se_track, mbi_track)
         values ('$firstname', '$lastname', '$sid', '$birthday', '$accepted_on', 
            '$toefl', '$gre_verbal', '$gre_quant', '$gre_aw', '$university', 
            '$grade', '$country', '$email', '$db_track', '$ds_track', 
            '$is_track', '$oo_track', '$se_track', '$mbi_track')";
      $result = my_mysql_query($query);  
      $primaryKey = mysql_insert_id();
      
      // Create a list of allowable courses in XML.  The list of legal
      // courses is driven by the approved tracks, but a list of common
      // courses.
      $allowable = ""; //"<allowablecourses>";
      $allowable .= '<course label="CS 500" />';
      if ( $db_track ) {
         $allowable .= '<course label="CS 673" />';
         $allowable .= '<course label="CS 676" />';
         $allowable .= '<course label="CS 679" />';         
      }
      if ( $ds_track ) {
         $allowable .= '<course label="CS 654" />';
         $allowable .= '<course label="CS 656" />';
         $allowable .= '<course label="CS 658" />';         
      }
      if ( $is_track ) {
         $allowable .= '<course label="CS 641" />';
         $allowable .= '<course label="CS 642" />';
         $allowable .= '<course label="CS 643" />';         
      }
      if ( $oo_track ) {
         $allowable .= '<course label="CS 621" />';
         $allowable .= '<course label="CS 622" />';
         $allowable .= '<course label="CS 623" />';         
      }
      if ( $se_track ) {
         $allowable .= '<course label="CS 611" />';
         $allowable .= '<course label="CS 612" />';
         $allowable .= '<course label="CS 613" />';         
      }
      if ( $mbi_track ) {
         $allowable .= '<course label="CS 661" />';
         $allowable .= '<course label="CS 635" />';
         $allowable .= '<course label="CS 665" />';         
         $allowable .= '<course label="CS 671" />';         
         $allowable .= '<course label="CS 677" />';         
         $allowable .= '<course label="CS 678" />';         
      }
      $allowable .= '<course label="CS 692" />';         
      $allowable .= '<course label="CS 693" />';         
      $allowable .= '<course label="CS 690" />';         
      $allowable .= '<course label="CS 695" />';         
    //  $allowable .= "</allowablecourses>"; 
      
      // Create an initial plan of study that has only semesters listed.
      $year = date("Y"); // get current year
      $plan = ""; // "<planofstudy>";
      for ($i = 0; $i < 8; $i++) {
         $plan .= "<semester label=\"Fall $year\" />";
         $plan .= "<semester label=\"Winter $year\" />";
         $year++;
      }
     // $plan .= "</planofstudy>";
      
      // Store the list of allowable courses in the database.
      $query = "insert into gs_plans (student_id, allowable, xml) values ($primaryKey, '$allowable', '$plan')";
      $result = my_mysql_query($query); 

   }
   
   // Or are we updating an existing record?
   else {       
      $query = "update gs_students set 
         firstname='$firstname', 
         lastname='$lastname', 
         sid='$sid', 
         birthday='$birthday', 
         accepted_on='$accepted_on', 
         toefl='$toefl', 
         gre_verbal='$gre_verbal', 
         gre_quant='$gre_quant', 
         gre_aw='$gre_aw', 
         university='$university', 
         grade='$grade', 
         country='$country', 
         email='$email', 
         db_track='$db_track', 
         ds_track='$ds_track', 
         is_track='$is_track', 
         oo_track='$oo_track', 
         se_track='$se_track', 
         mbi_track='$mbi_track'
         where id=$primaryKey";
      $result = my_mysql_query($query);      
   }
}

?>